#ifndef _sockets_h
#define _sockets_h

#ifdef WIN32

	#include <windows.h>
	/*
	
	The Windows code will have to be made with Winsock. It's all more
	or less identical to the Linux sockets, fortunately. All that is
	required is the renaming of datatypes and functions ;)
	
	I can write it for you if you like. I have done it before.
	
	*/

#else /*! linux */

	#include <arpa/inet.h>
	#include <fcntl.h> /// For some file manipulation functions
	#include <netdb.h>
	#include <netinet/in.h>
	#include <unistd.h>
	#include <sys/file.h>
	#include <sys/socket.h>
	#include <sys/time.h>
	#include <sys/types.h>
	#include <pthread.h> /// Linux threads, in case we deicde to use

#endif /*! WIN32 */

namespace server
{
	/** \brief Initialize the server.
    *
    * Prepares all the variables for starting a server.
    * Will throw exception on error.
    *
    * \return 0 on success
    */
	int init();
	
	/** \brief Start the server.
	 * 
	 * Causes the server to start listening for connections
	 * and then serves a connected client. Server may run in a thread
	 * independant of the pong game itself, so that simply-pong doesn't
	 * hang until someone connects.
	 * 
	 * Alternatively, we can implement code into the main game loop.
	 * 
	 * \return true on success.
	 * \return false on failure.
	 */
	bool start();
	/** \brief Stop the server.
	 * 
	 * Causes the server to stop listening for clients and/or stop
	 * serving clients already connected.
	 */
	void stop();
}

namespace client
{
	/** \brief Initialize the client.
    *
    * Prepares all the variables for connecting to a server.
    * Will throw exception on error.
    *
    * \return 0 on success.
    */
	int init();
	
	/** \brief Connect to a server.
    *
    * Attempts to connect to the server.
    * Will throw exception on error.
    *
    * \param serverip IP of the server as dotted quad (eg. "127.0.0.1")
    * \param port Port of the server.
    * 
    * \return 0 on success.
    * \return 1 on failure.
    * \return -1 on a server which isn't a valid SimpyPong server.
    */
	int connect(char *serverip, int port);
	
	/** \brief Disconnect from server */
	void disconnect();
}

/// If the string sent from the server to the client is correct, and
/// if the string sent from the client to the server is correct, then we
/// know that legitimate SimplyPong clients and servers are being used.
/// It is a mild defence against hackers and arseholes on the internet.
// Sent from server to client
int validation_string_a[18] = { 'S', 'i', 'm', 'p', 'l', 'y', 'P', 'o', 'n', 'g', 'S', 'e', 'r', 'v', 'e', 'r', 'v', '1' };
//Sent from client to server
int validation_string_b[18] = { 'S', 'i', 'm', 'p', 'l', 'y', 'P', 'o', 'n', 'g', 'C', 'l', 'i', 'e', 'n', 't', 'v', '1' };
/// The string is filtered through a cipher to produce a proper message.
/// Add 13 to the characters in the string to restore the proper letters.
#define ignore // i don't know why this shit won't work...
#ifndef ignore

int iCount = 0;

for(; iCount<(sizeof(validation_string_a)/sizeof(int)); iCount++)
{
	validation_string_a[iCount] -= 13;
}

for(iCount=0; iCount<(sizeof(validation_string_b)/sizeof(int)); iCount++)
{
	validation_string_b[iCount] -= 13;
}
/// That's our simple security done :D
#endif // ignore

#endif /*! _sockets_h */
